
using System.Collections.Generic;

using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using ColorPicker;
using CookComputing.XmlRpc;
using MS.WindowsAPICodePack.Internal;
using My.Resources;
using Microsoft.VisualBasic;

using System.Collections;

using System.Diagnostics;

using System;

using System.Speech.Synthesis;
using System.Collections.ObjectModel;
using Microsoft.VisualBasic.Devices;
using Microsoft.Windows.Controls.Ribbon;
using Microsoft.WindowsAPICodePack;

using Microsoft.WindowsAPICodePack.Taskbar;
using Microsoft.WindowsAPICodePack.Dialogs;
using Keyboard = System.Windows.Input.Keyboard;


public partial class MainWindow
{

    private DocumentTab SelectedDocument = null;
    private SpeechSynthesizer Speech = new SpeechSynthesizer();
    //private bool IsJumpListAdded = false;

    #region Reuseable Code

    #region Document


    public MainWindow()
    {
        Initialized += new EventHandler(MainWindow_Initialized);

        Loaded += new RoutedEventHandler(MainWindow_Loaded);
        Activated += new EventHandler(MainWindow_Activated);
        KeyDown += new KeyEventHandler(MainWindow_KeyDown);
        Closing += new System.ComponentModel.CancelEventHandler(MainWindow_Closing);
        InitializeComponent();

    }
    public void NewDocument(string title)
    {
        if (TabCell.Items.Count > 0)
        {
            SelectedDocument.IsSelected = false;
        }
        DocumentTab tb = new DocumentTab(title);
        TabCell.Items.Add(tb);
        ResizeTabs(TabCell);
        tb.IsSelected = true;
        SelectedDocument.Editor.Focus();
        SelectedDocument.Editor.FileChanged = false;
        if (MS.WindowsAPICodePack.Internal.CoreHelpers.RunningOnWin7)
        {
            var tab = new TabbedThumbnail(this, tb.Editor, new Vector(0, 0));
            tab.SetWindowIcon(My.Resources.Resources.open16);
            tab.Title = tb.DocName;
            tab.TabbedThumbnailActivated += preview_TabbedThumbnailActivated;
            tab.TabbedThumbnailClosed += preview_TabbedThumbnailClosed;
            tab.TabbedThumbnailMaximized += preview_TabbedThumbnailMaximized;
            tab.TabbedThumbnailMinimized += preview_TabbedThumbnailMinimized;
            TaskbarManager.Instance.TabbedThumbnail.AddThumbnailPreview(tab);
        }
        UpdateButtons(this, null);
    }

    private void CloseDocument(DocumentTab file)
    {
        if( CoreHelpers.RunningOnWin7 )
        {
            var tab = TaskbarManager.Instance.TabbedThumbnail.GetThumbnailPreview(file.Editor);
            if(TaskbarManager.Instance.TabbedThumbnail.IsThumbnailPreviewAdded(tab))
            {
                tab.TabbedThumbnailActivated -= preview_TabbedThumbnailActivated;
                tab.TabbedThumbnailClosed -= preview_TabbedThumbnailClosed;
                tab.TabbedThumbnailMaximized -= preview_TabbedThumbnailMaximized;
                tab.TabbedThumbnailMinimized -= preview_TabbedThumbnailMinimized;
                TaskbarManager.Instance.TabbedThumbnail.RemoveThumbnailPreview(file.Editor);
            }
        }
        TabCell.Items.Remove(file);
        ResizeTabs(TabCell);
    }

    public CommonFileDialogFilterCollection GetOpenDialogFilters(CommonOpenFileDialog dialog)
    {
        CommonFileDialogFilterCollection f = dialog.Filters;
        var SupportedFiles = new Microsoft.WindowsAPICodePack.Dialogs.CommonFileDialogFilter("Supported Documents", "*.xaml;*.html;*.htm;*.rtf;*.txt");
        var FlowDocumentFiles = new Microsoft.WindowsAPICodePack.Dialogs.CommonFileDialogFilter("FlowDocument", "*.xaml");
        var HtmlFiles = new Microsoft.WindowsAPICodePack.Dialogs.CommonFileDialogFilter("Html Document", "*.html;*.htm");
        var RichTextFiles = new Microsoft.WindowsAPICodePack.Dialogs.CommonFileDialogFilter("Rich Text Document", "*.rtf");
        var PlanTextFiles = new Microsoft.WindowsAPICodePack.Dialogs.CommonFileDialogFilter("Plan Text Document", "*.txt");
        var AllFiles = new Microsoft.WindowsAPICodePack.Dialogs.CommonFileDialogFilter("All Files", "*.*");
        f = dialog.Filters;
        f.Add(SupportedFiles);
        f.Add(FlowDocumentFiles);
        f.Add(HtmlFiles);
        f.Add(RichTextFiles);
        f.Add(PlanTextFiles);
        f.Add(AllFiles);
        return f;
    }

    #endregion

    public void ResizeTabs(TabControl Tab)
    {
        if (MySettings.Default.Options_Tabs_SizeMode == 0)
        {
            foreach (DocumentTab i in Tab.Items)
            {
                if (TabCell.Items.Count > 8)
                {
                    double d = new double();
                    double d2 = new double();
                    d = TabCell.ActualWidth - 8;
                    d2 = TabCell.Items.Count * 2;
                    i.Width = d / d2;
                }
                else
                {
                    double d = new double();
                    d = Tab.ActualWidth;
                    i.Width = d / 8;
                }
            }
        }
    }



    #region -RibbonBar

    private void UpdateSelected()
    {
        if (SelectedDocument.Editor.FileChanged)
        {
            Title = SelectedDocument.DocName + " * - " +  My.MyWpfExtension.Application.Info.ProductName;
        }
        else
        {
            Title = SelectedDocument.DocName + " - " + (My.MyWpfExtension.Application.Info.ProductName);
        }
        MainBar.Title = Title;
        if (My.MyWpfExtension.Computer.FileSystem.FileExists(SelectedDocument.Editor.DocumentName))
        {
            double kb = System.Convert.ToDouble(My.MyWpfExtension.Computer.FileSystem.GetFileInfo(SelectedDocument.Editor.DocumentName).Length / 1024);
            int inter = Convert.ToInt32(kb);
            //SizeButton.Content = "Size: " + inter.ToString() + " KB";
        }
        else
        {
            //SizeButton.Content = "Size: " + "0 KB";
        }
        TextPointer ls = SelectedDocument.Editor.CaretPosition.GetLineStartPosition(0);
        TextPointer p = SelectedDocument.Editor.Document.ContentStart.GetLineStartPosition(0);
        int @int = 1;
        int int2 = 1;
        while (true)
        {
            if (ls.CompareTo(p) < 1)
            {
                break;
            }
            int r;
            p = p.GetLineStartPosition(1, out r);
            if (r == 0)
            {
                break;
            }
            @int++;
        }
        TextPointer ls2 = SelectedDocument.Editor.Document.ContentStart.DocumentEnd.GetLineStartPosition(0);
        TextPointer p2 = SelectedDocument.Editor.Document.ContentEnd.DocumentStart.GetLineStartPosition(0);
        while (true)
        {
            if (ls2.CompareTo(p2) < 1)
            {
                break;
            }
            int r;
            p2 = p2.GetLineStartPosition(1, out r);
            if (r == 0)
            {
                break;
            }
            int2++;
        }
        TextRange t = new TextRange(SelectedDocument.Editor.Document.ContentStart, SelectedDocument.Editor.Document.ContentEnd);
        //LineButton.Content = "Words: " + SelectedDocument.Editor.GetWordCount().ToString();
        //LineButton.Content = LineButton.Content + ", Line: " + @int.ToString() + "/" + int2.ToString();
        TextPointer caretPos = SelectedDocument.Editor.CaretPosition;
        TextPointer poi = SelectedDocument.Editor.CaretPosition.GetLineStartPosition(0);
        int currentColumnNumber = System.Convert.ToInt32(Math.Max(System.Convert.ToInt32(p.GetOffsetToPosition(caretPos) - 1), 0) + 1);
        int ColumnCount = currentColumnNumber;
        ColumnCount += SelectedDocument.Editor.CaretPosition.GetTextRunLength(System.Windows.Documents.LogicalDirection.Forward);
        //LineButton.Content = LineButton.Content + ", Column: " + currentColumnNumber.ToString() + "/" + ColumnCount.ToString();
        if (SelectedDocument.Editor.CanUndo)
        {
            CommonUndoButton.IsEnabled = true;
            UndoButton.IsEnabled = true;
        }
        else
        {
            CommonUndoButton.IsEnabled = false;
            UndoButton.IsEnabled = false;
        }
        if (SelectedDocument.Editor.CanRedo)
        {
            CommonRedoButton.IsEnabled = true;
            RedoButton.IsEnabled = true;
        }
        else
        {
            CommonRedoButton.IsEnabled = false;
            RedoButton.IsEnabled = false;
        }
        if (My.MyWpfExtension.Computer.Clipboard.ContainsText())
        {
            CommonPasteButton.IsEnabled = true;
            CommonPasteButton.ToolTipFooterTitle = "Preview:";
            CommonPasteButton.ToolTipFooterDescription = My.MyWpfExtension.Computer.Clipboard.GetText();
            PasteButton.IsEnabled = true;
            PasteButton.ToolTipFooterTitle = "Preview:";
            PasteButton.ToolTipFooterDescription = My.MyWpfExtension.Computer.Clipboard.GetText();
            PasteTextButton.IsEnabled = true;
        }
        else if (My.MyWpfExtension.Computer.Clipboard.ContainsImage())
        {
            //TODO: add paste image support
        }
        else
        {
            PasteTextButton.IsEnabled = false;
            CommonPasteButton.IsEnabled = false;
            CommonPasteButton.ToolTipFooterTitle = null;
            CommonPasteButton.ToolTipFooterDescription = null;
            PasteButton.IsEnabled = false;
            PasteButton.ToolTipFooterTitle = null;
            PasteButton.ToolTipFooterDescription = null;
        }
        if (SelectedDocument.Editor.FileChanged)
        {
            SaveMenuItem.IsEnabled = true;
            SaveButton.IsEnabled = true;
        }
        else
        {
            SaveMenuItem.IsEnabled = false;
            SaveButton.IsEnabled = false;
        }
    }

    private void UpdateButtons(object sender, RoutedEventArgs routedEventArgs)
    {
        if (TabCell.SelectedIndex != -1)
        {
            UpdateSelected();
            foreach (DocumentTab i in TabCell.Items)
            {
                i.ShowCloseButton();
            }
            //SizeButton.Visibility = System.Windows.Visibility.Visible;
            //LineButton.Visibility = System.Windows.Visibility.Visible;
            CloseMenuItem.IsEnabled = true;
            CloseAllMenuItem.IsEnabled = true;
            CloseAllButton.IsEnabled = true;
            SaveAsMenuItem.IsEnabled = true;
            SaveAsButton.IsEnabled = true;
            SaveAllMenuItem.IsEnabled = true;
            SaveAllButton.IsEnabled = true;
            ExportMenuItem.IsEnabled = true;
            PropertiesMenuItem.IsEnabled = true;



            EditTab.IsEnabled = true;
            ZoomInButton.IsEnabled = true;
            if (SelectedDocument.Editor.ZoomLevel >= 0.2)
            {
                ZoomOutButton.IsEnabled = true;
            }
            else
            {
                ZoomOutButton.IsEnabled = false;
            }
            ResetZoomButton.IsEnabled = true;
            InsertTab.IsEnabled = true;
            FormatTab.IsEnabled = true;

            CommonEditGroup.IsEnabled = true;
            CommonInsertGroup.IsEnabled = true;
            CommonFormatGroup.IsEnabled = true;
            CommonSpellCheckButton.IsEnabled = true;

            SpellCheckButton.IsEnabled = true;

        }
        else if (TabCell.SelectedIndex == -1)
        {
            SelectedDocument = null;
            Title = My.MyWpfExtension.Application.Info.ProductName;
            MainBar.Title = My.MyWpfExtension.Application.Info.ProductName;
            //SizeButton.Visibility = System.Windows.Visibility.Collapsed;
            //LineButton.Visibility = System.Windows.Visibility.Collapsed;
            MainBar.SelectedIndex = 0;
            CloseMenuItem.IsEnabled = false;
            CloseAllMenuItem.IsEnabled = false;
            CloseAllButton.IsEnabled = false;
            SaveMenuItem.IsEnabled = false;
            SaveButton.IsEnabled = false;
            SaveAsMenuItem.IsEnabled = false;
            SaveAsButton.IsEnabled = false;
            SaveAllMenuItem.IsEnabled = false;
            SaveAllButton.IsEnabled = false;
            ExportMenuItem.IsEnabled = false;
            PropertiesMenuItem.IsEnabled = false;
            EditTab.IsEnabled = false;
            ZoomInButton.IsEnabled = false;
            ZoomOutButton.IsEnabled = false;
            ResetZoomButton.IsEnabled = false;
            InsertTab.IsEnabled = false;
            FormatTab.IsEnabled = false;
            CommonPasteButton.ToolTipFooterTitle = null;
            CommonPasteButton.ToolTipFooterDescription = null;
            CommonEditGroup.IsEnabled = false;
            CommonInsertGroup.IsEnabled = false;
            CommonFormatGroup.IsEnabled = false;
            CommonSpellCheckButton.IsEnabled = false;

            SpellCheckButton.IsEnabled = false;

        }
    }

    #endregion

    #endregion

    #region MainWindow

    #region Add Handlers

    private void addhandlers()
    {
        this.AddHandler(TabHeader.CloseTabEvent, new RoutedEventHandler(CloseDoc));
        this.AddHandler(TabHeader.SaveTabEvent, new RoutedEventHandler(SaveMenuItem_Click));
        this.AddHandler(TabHeader.SaveAsTabEvent, new RoutedEventHandler(SaveAsMenuItem_Click));
        this.AddHandler(DocumentTab.UpdateSelected, new RoutedEventHandler(UpdateButtons));
        this.AddHandler(DocumentTab.UndoEvent, new RoutedEventHandler(UndoMenuItem_Click));
        this.AddHandler(DocumentTab.RedoEvent, new RoutedEventHandler(RedoMenuItem_Click));
        this.AddHandler(DocumentTab.CutEvent, new RoutedEventHandler(CutMenuItem_Click));
        this.AddHandler(DocumentTab.CopyEvent, new RoutedEventHandler(CopyMenuItem_Click));
        this.AddHandler(DocumentTab.PasteEvent, new RoutedEventHandler(PasteMenuItem_Click));
        this.AddHandler(DocumentTab.DeleteEvent, new RoutedEventHandler(DeleteMenuItem_Click));
        this.AddHandler(DocumentTab.SelectAllEvent, new RoutedEventHandler(SelectAllMenuItem_Click));
        this.AddHandler(DocumentTab.FindEvent, new RoutedEventHandler(FindButton_Click));
        this.AddHandler(DocumentTab.ReplaceEvent, new RoutedEventHandler(ReplaceButton_Click));
        this.AddHandler(DocumentTab.GoToEvent, new RoutedEventHandler(GoToButton_Click));
    }

  

    #endregion

    #region Activated

    public void MainWindow_Activated(object sender, System.EventArgs e)
    {
        
    }

    #endregion

    #region Key Down

    public void MainWindow_KeyDown(object sender, KeyEventArgs e)
    {
        if (TabCell.SelectedContent != null)
        {
            if (Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl) && !Keyboard.IsKeyDown(Key.LeftShift))
            {
                if (e.Key == Key.F)
                {
                    e.Handled = true;
                    FindButton_Click(null, null);
                }
                else if (e.Key == Key.G)
                {
                    e.Handled = true;
                    GoToButton_Click(null, null);
                }
                else if (e.Key == Key.H)
                {
                    e.Handled = true;
                    ReplaceButton_Click(null, null);
                }
                else if (e.Key == Key.N)
                {
                    NewMenuItem_Click(null, null);
                }
                else if (e.Key == Key.O)
                {
                    e.Handled = true;
                    OpenMenuItem_Click(null, null);
                }

                else if (e.Key == Key.S)
                {
                    e.Handled = true;
                    if (SaveMenuItem.IsEnabled)
                    {
                        SaveMenuItem_Click(null, null);
                    }
                }
            }
            if (Keyboard.IsKeyDown(Key.Insert))
            {

                if (e.Key == Key.H)
                {
                    e.Handled = true;
                    HorizontalLineButton_Click(null, null);
                }
                else if (e.Key == Key.I)
                {
                    e.Handled = true;
                    ImageButton_Click(null, null);
                }
                else if (e.Key == Key.L)
                {
                    e.Handled = true;
                    LinkMenuItem_Click(null, null);
                }

                else if (e.Key == Key.T)
                {
                    e.Handled = true;
                    TableButton_Click(null, null);
                }

                else if (e.Key == Key.X)
                {
                    e.Handled = true;
                    TextFileButton_Click(null, null);
                }
            }
            if (e.Key == Key.F1)
            {
                MainBar.SelectedIndex = 0;
            }
            else if (e.Key == Key.F2)
            {
                MainBar.SelectedIndex = 1;
            }
            else if (e.Key == Key.F3)
            {
                MainBar.SelectedIndex = 2;
            }
            else if (e.Key == Key.F4)
            {
                MainBar.SelectedIndex = 3;
            }
            else if (e.Key == Key.F5)
            {
                MainBar.SelectedIndex = 4;
            }
            else if (e.Key == Key.F6)
            {
                MainBar.SelectedIndex = 5;
            }
            else if (e.Key == Key.F7)
            {
                MainBar.SelectedIndex = 6;
            }
            else if (e.Key == Key.F8)
            {
                SpellCheckButton_Click(null, null);
            }

        }
        else
        {
            if (Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl) && !Keyboard.IsKeyDown(Key.LeftShift))
            {
                if (e.Key == Key.N)
                {
                    NewMenuItem_Click(null, null);
                }
                else if (e.Key == Key.O)
                {
                    e.Handled = true;
                    OpenMenuItem_Click(null, null);
                }
            }
            if (e.Key == Key.F1)
            {
                MainBar.SelectedIndex = 0;
            }
            else if (e.Key == Key.F6)
            {
                MainBar.SelectedIndex = 5;
            }
            else if (e.Key == Key.F7)
            {
                MainBar.SelectedIndex = 6;
            }
        }
    }

    #endregion

    #region Initialized

    public void MainWindow_Initialized(object sender, System.EventArgs e)
    {
        var t = new System.Threading.Thread(new System.Threading.ThreadStart(addhandlers));
      
        t.Start();

        this.Show();
        foreach (string file in (new Computer()).FileSystem.GetFiles(My.MyWpfExtension.Application.Info.DirectoryPath + "\\Templates\\"))
        {
            var template = new System.IO.FileInfo(file);
            var item = new RibbonApplicationMenuItem();
            item.Header = template.Name.Remove(template.Name.Length - 5);
            item.ToolTipDescription = file;
            item.Click += new System.Windows.RoutedEventHandler(new RoutedEventHandler(NewFromTemplate));
            NewMenuItem.Items.Add(item);
        }

    }

    #endregion

    #region Loaded

    public void MainWindow_Loaded(object sender, RoutedEventArgs e)
    {
        if (MySettings.Default.MainWindow_IsMax)
        {
            this.WindowState = System.Windows.WindowState.Maximized;
        }
        else
        {
            this.Left = System.Convert.ToDouble(MySettings.Default.MainWindow_Loc.X);
            this.Top = System.Convert.ToDouble(MySettings.Default.MainWindow_Loc.Y);
            this.Height = System.Convert.ToDouble(MySettings.Default.MainWindow_Size.Height);
            this.Width = System.Convert.ToDouble(MySettings.Default.MainWindow_Size.Width);
        }
        if (MySettings.Default.MainWindow_HideRibbonbar)
        {
            MainBar.IsMinimized = true;
        }
        else
        {
            MainBar.IsMinimized = false;
        }
        if (MySettings.Default.Options_ShowRecentDocuments && MySettings.Default.Options_RecentFiles.Count > 0)
        {
            foreach (string s in MySettings.Default.Options_RecentFiles)
            {
                if (My.MyWpfExtension.Computer.FileSystem.FileExists(s))
                {
                    var i2 = new RibbonApplicationMenuItem();
                    var f = new System.IO.FileInfo(s);
                    var ContMenu = new ContextMenu();
                    var removemenuitem = new MenuItem();
                    removemenuitem.Header = "Remove";
                    removemenuitem.ToolTip = f.FullName;
                    ContMenu.Items.Add(removemenuitem);
                    i2.ContextMenu = ContMenu;
                    i2.Header = f.Name;
                    i2.ToolTipTitle = f.Name;
                    i2.ToolTipDescription = s;
                    RecentFilesList.Children.Add(i2);
                    i2.Click += new RoutedEventHandler(new RoutedEventHandler(recentfile_click));
                    removemenuitem.Click += new RoutedEventHandler(new RoutedEventHandler(recentfileremove_click));
                }
            }
        }
        if (MySettings.Default.MainWindow_QAToolbar_New)
        {
            ShowNewMenuItem.IsChecked = true;
            NewButton.Visibility = System.Windows.Visibility.Visible;
        }
        else
        {
            ShowOpenMenuItem.IsChecked = false;
            NewButton.Visibility = System.Windows.Visibility.Collapsed;
        }
        if (MySettings.Default.MainWindow_QAToolbar_Open)
        {
            ShowOpenMenuItem.IsChecked = true;
            OpenButton.Visibility = System.Windows.Visibility.Visible;
        }
        else
        {
            ShowOpenMenuItem.IsChecked = false;
            OpenButton.Visibility = System.Windows.Visibility.Collapsed;
        }
        if (MySettings.Default.MainWindow_QAToolbar_CloseAll)
        {
            ShowCloseAllMenuItem.IsChecked = true;
            CloseAllButton.Visibility = System.Windows.Visibility.Visible;
        }
        else
        {
            ShowCloseAllMenuItem.IsChecked = false;
            CloseAllButton.Visibility = System.Windows.Visibility.Collapsed;
        }
        if (MySettings.Default.MainWindow_QAToolbar_Save)
        {
            ShowSaveMenuItem.IsChecked = true;
            SaveButton.Visibility = System.Windows.Visibility.Visible;
        }
        else
        {
            ShowSaveMenuItem.IsChecked = false;
            SaveButton.Visibility = System.Windows.Visibility.Collapsed;
        }
        if (MySettings.Default.MainWindow_QAToolbar_SaveAs)
        {
            ShowSaveAsMenuItem.IsChecked = true;
            SaveAsButton.Visibility = System.Windows.Visibility.Visible;
        }
        else
        {
            ShowSaveAsMenuItem.IsChecked = false;
            SaveAsButton.Visibility = System.Windows.Visibility.Collapsed;
        }
        if (MySettings.Default.MainWindow_QAToolbar_SaveAll)
        {
            ShowSaveAllMenuItem.IsChecked = true;
            SaveAllButton.Visibility = System.Windows.Visibility.Visible;
        }
        else
        {
            ShowSaveAllMenuItem.IsChecked = false;
            SaveAllButton.Visibility = System.Windows.Visibility.Collapsed;
        }
        if (MySettings.Default.MainWindow_QAToolbar_Export)
        {
            ShowExportMenuItem.IsChecked = true;
            ExportButton.Visibility = System.Windows.Visibility.Visible;
        }
        else
        {
            ShowExportMenuItem.IsChecked = false;
            ExportButton.Visibility = System.Windows.Visibility.Collapsed;
        }


        RibbonbarMenuItem.IsChecked = Convert.ToBoolean(MySettings.Default.MainWindow_HideRibbonbar);
        if (MySettings.Default.MainWindow_IsRibbonBelow)
        {
            MainMenuMenuItem.IsChecked = true;
            MainBar.ShowQuickAccessToolBarOnTop = false;
        }
        else
        {
            MainMenuMenuItem.IsChecked = false;
            MainBar.ShowQuickAccessToolBarOnTop = true;
        }

        try
        {
            if (Application.StartUpFileNames.Count > 0)
            {
                if (MySettings.Default.Options_StartupMode == 1 && MySettings.Default.Options_PreviousDocuments.Count > 0)
                {
                    CloseMenuItem_Click(this, null);
                    foreach (string s in MySettings.Default.Options_PreviousDocuments)
                    {
                        if (My.MyWpfExtension.Computer.FileSystem.FileExists(s))
                        {
                            var f = new System.IO.FileInfo(s);
                            NewDocument(f.Name);
                            SelectedDocument.Editor.LoadDocument(f.FullName);
                        }
                    }
                }
                foreach (string s in Application.StartUpFileNames)
                {
                    var f = new System.IO.FileInfo(s);
                    NewDocument(f.Name);
                    SelectedDocument.Editor.LoadDocument(f.FullName);
                }
            }
            else
            {
                NewMenuItem_Click(this, null);
                if (MySettings.Default.Options_StartupMode == 1 && MySettings.Default.Options_PreviousDocuments.Count > 0)
                {
                    CloseMenuItem_Click(this, null);
                    foreach (string s in MySettings.Default.Options_PreviousDocuments)
                    {
                        if (My.MyWpfExtension.Computer.FileSystem.FileExists(s))
                        {
                            var f = new System.IO.FileInfo(s);
                            NewDocument(f.Name);
                            SelectedDocument.Editor.LoadDocument(f.FullName);
                        }
                    }
                }
                else if (MySettings.Default.Options_StartupMode == 2)
                {
                    CloseMenuItem_Click(this, null);
                    OpenMenuItem_Click(this, null);
                }
                else if (MySettings.Default.Options_StartupMode == 3)
                {
                    CloseMenuItem_Click(this, null);
                }
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message, ex.ToString());
        }
 
    }

    #endregion

    #region Closing

    public void MainWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
    {
        if (this.WindowState == System.Windows.WindowState.Maximized)
        {
            MySettings.Default.MainWindow_IsMax = true;
        }
        else if (this.WindowState == System.Windows.WindowState.Normal)
        {
            MySettings.Default.MainWindow_IsMax = false;
            int wi;
            int hi;
            int le;
            int ti;
            wi = Convert.ToInt32(this.ActualWidth);
            hi = Convert.ToInt32(this.ActualHeight);
            le = Convert.ToInt32(Left);
            ti = Convert.ToInt32(Top);
            var s = new System.Windows.Size(wi, hi);
            var l = new System.Windows.Point(le, ti);
            MySettings.Default.MainWindow_Size = s;
            MySettings.Default.MainWindow_Loc = l;
        }
        MySettings.Default.MainWindow_HideRibbonbar = RibbonbarMenuItem.IsChecked;
        MySettings.Default.MainWindow_QAToolbar_New = ShowNewMenuItem.IsChecked;
        MySettings.Default.MainWindow_QAToolbar_Open = ShowOpenMenuItem.IsChecked;
        MySettings.Default.MainWindow_QAToolbar_CloseAll = ShowCloseAllMenuItem.IsChecked;
        MySettings.Default.MainWindow_QAToolbar_Save = ShowSaveMenuItem.IsChecked;
        MySettings.Default.MainWindow_QAToolbar_SaveAs = ShowSaveAsMenuItem.IsChecked;
        MySettings.Default.MainWindow_QAToolbar_SaveAll = ShowSaveAllMenuItem.IsChecked;
        MySettings.Default.MainWindow_QAToolbar_Export = ShowExportMenuItem.IsChecked;

        if (MySettings.Default.Options_StartupMode == 1 && MySettings.Default.Options_PreviousDocuments.Count > 0)
        {
            MySettings.Default.Options_PreviousDocuments.Clear();
        }
        while (TabCell.Items.Count > 0)
        {
            if (SelectedDocument.Editor.FileChanged)
            {
                var save = new SaveFileDialog();
                save.Owner = this;
                save.SetFileInfo(SelectedDocument.DocName, SelectedDocument.Editor);
                System.IO.FileStream fs = System.IO.File.Open(My.MyWpfExtension.Computer.FileSystem.SpecialDirectories.Temp + "\\TVPre.xaml", System.IO.FileMode.Create);
                var tr = new TextRange(SelectedDocument.Editor.Document.ContentStart, SelectedDocument.Editor.Document.ContentEnd);
                System.Windows.Markup.XamlWriter.Save(SelectedDocument.Editor.Document, fs);
                fs.Close();
                MySettings.Default.Options_PreviousDocuments.Clear();
                save.ShowDialog();
                if (save.Res == "Yes")
                {
                    if (SelectedDocument.Editor.DocumentName != null)
                    {
                        SelectedDocument.Editor.SaveDocument(SelectedDocument.Editor.DocumentName);
                        MySettings.Default.Options_PreviousDocuments.Add(SelectedDocument.Editor.DocumentName);
                        CloseDocument(SelectedDocument);
                    }
                    else
                    {
                        SaveMenuItem_Click(null, null);
                        if (SelectedDocument.Editor.DocumentName != null)
                        {
                            MySettings.Default.Options_PreviousDocuments.Add(SelectedDocument.Editor.DocumentName);
                        }
                    }
                }
                else if (save.Res == "No")
                {
                    if (SelectedDocument.Editor.DocumentName != null)
                    {
                        MySettings.Default.Options_PreviousDocuments.Add(SelectedDocument.Editor.DocumentName);
                    }
                    CloseDocument(SelectedDocument);
                }
                else
                {
                    e.Cancel = true;
                    break;
                }
            }
            else
            {
                if (SelectedDocument.Editor.DocumentName != null)
                {
                    MySettings.Default.Options_PreviousDocuments.Add(SelectedDocument.Editor.DocumentName);
                }
                CloseDocument(SelectedDocument);
            }
        }
        MySettings.Default.Save();
    }

    #endregion

    #endregion

    #region -QAToolbar

    public void ShowNewMenuItem_Click(object sender, RoutedEventArgs e)
    {
        if (ShowNewMenuItem.IsChecked)
        {
            NewButton.Visibility = System.Windows.Visibility.Visible;
        }
        else
        {
            NewButton.Visibility = System.Windows.Visibility.Collapsed;
        }
    }

    public void ShowOpenMenuItem_Click(object sender, RoutedEventArgs e)
    {
        if (ShowOpenMenuItem.IsChecked)
        {
            OpenButton.Visibility = System.Windows.Visibility.Visible;
        }
        else
        {
            OpenButton.Visibility = System.Windows.Visibility.Collapsed;
        }
    }

    public void ShowCloseAllMenuItem_Click(object sender, RoutedEventArgs e)
    {
        if (ShowCloseAllMenuItem.IsChecked)
        {
            CloseAllButton.Visibility = System.Windows.Visibility.Visible;
        }
        else
        {
            CloseAllButton.Visibility = System.Windows.Visibility.Collapsed;
        }
    }

    public void ShowSaveMenuItem_Click(object sender, RoutedEventArgs e)
    {
        if (ShowSaveMenuItem.IsChecked)
        {
            SaveButton.Visibility = System.Windows.Visibility.Visible;
        }
        else
        {
            SaveButton.Visibility = System.Windows.Visibility.Collapsed;
        }
    }

    public void ShowSaveAsMenuItem_Click(object sender, RoutedEventArgs e)
    {
        if (ShowSaveAsMenuItem.IsChecked)
        {
            SaveAsButton.Visibility = System.Windows.Visibility.Visible;
        }
        else
        {
            SaveAsButton.Visibility = System.Windows.Visibility.Collapsed;
        }
    }

    public void ShowSaveAllMenuItem_Click(object sender, RoutedEventArgs e)
    {
        if (ShowSaveAllMenuItem.IsChecked)
        {
            SaveAllButton.Visibility = System.Windows.Visibility.Visible;
        }
        else
        {
            SaveAllButton.Visibility = System.Windows.Visibility.Collapsed;
        }
    }

    public void ShowExportMenuItem_Click(object sender, RoutedEventArgs e)
    {
        if (ShowExportMenuItem.IsChecked)
        {
            ExportButton.Visibility = System.Windows.Visibility.Visible;
        }
        else
        {
            ExportButton.Visibility = System.Windows.Visibility.Collapsed;
        }
    }



    public void MainMenuMenuItem_Click(object sender, RoutedEventArgs e)
    {
        if (MainMenuMenuItem.IsChecked)
        {
            MySettings.Default.MainWindow_IsRibbonBelow = true;
            MainBar.ShowQuickAccessToolBarOnTop = false;
        }
        else
        {
            MySettings.Default.MainWindow_IsRibbonBelow = false;
            MainBar.ShowQuickAccessToolBarOnTop = true;
        }
    }

    public void RibbonbarMenuItem_Click(System.Object sender, RoutedEventArgs e)
    {
        if (RibbonbarMenuItem.IsChecked)
        {
            MainBar.IsMinimized = true;
        }
        else
        {
            MainBar.IsMinimized = false;
        }
    }

    #endregion

    #region --DocumentMenu

    private void recentfile_click(object sender, RoutedEventArgs e)
    {
        RibbonApplicationMenuItem i = (RibbonApplicationMenuItem)e.Source;
        System.IO.FileInfo f = new System.IO.FileInfo(i.ToolTipDescription);
        NewDocument("");
        SelectedDocument.Editor.LoadDocument(Convert.ToString(f.FullName));
    }

    private void recentfileremove_click(object sender, RoutedEventArgs e)
    {
        MenuItem i = (MenuItem)e.Source;
        RibbonApplicationMenuItem itemtoremove = null;
        foreach (RibbonApplicationMenuItem recentdoc in RecentFilesList.Children)
        {
            if (recentdoc.ToolTipDescription == i.ToolTip)
            {
                itemtoremove = recentdoc;
            }
        }
        string stringtoremove = null;
        foreach (string s in MySettings.Default.Options_RecentFiles)
        {
            if (s == i.ToolTip)
            {
                stringtoremove = s;
            }
        }
        MySettings.Default.Options_RecentFiles.Remove(stringtoremove);
        RecentFilesList.Children.Remove(itemtoremove);
    }

    #region New

    public void NewMenuItem_Click(System.Object sender, RoutedEventArgs e)
    {
        NewDocument("New Document");
        SelectedDocument.Editor.FileChanged = false;
    }

    private void NewFromTemplate(object sender, RoutedEventArgs e)
    {
        RibbonApplicationMenuItem t = (RibbonApplicationMenuItem)e.Source;
        System.IO.FileInfo template = new System.IO.FileInfo(t.ToolTipDescription);
        System.IO.FileStream fs = new System.IO.FileStream(template.FullName, System.IO.FileMode.Open);
        FlowDocument flow = new FlowDocument();
        flow = System.Windows.Markup.XamlReader.Load(fs) as FlowDocument;
        fs.Close();
        NewMenuItem_Click(null, null);
        SelectedDocument.Editor.Document = flow;
        SelectedDocument.Editor.DocumentName = null;
        SelectedDocument.Editor.FileChanged = false;
        e.Handled = true;
    }

    #endregion

    #region Open

    public void OpenMenuItem_Click(System.Object sender, RoutedEventArgs e)
    {
        if (MS.WindowsAPICodePack.Internal.CoreHelpers.RunningOnVista || MS.WindowsAPICodePack.Internal.CoreHelpers.RunningOnWin7)
        {
            Microsoft.WindowsAPICodePack.Dialogs.CommonOpenFileDialog openfile = new Microsoft.WindowsAPICodePack.Dialogs.CommonOpenFileDialog();
            openfile.Multiselect = true;
            GetOpenDialogFilters(openfile);
            if (openfile.ShowDialog() == Microsoft.WindowsAPICodePack.Dialogs.CommonFileDialogResult.Ok)
            {
                if (MS.WindowsAPICodePack.Internal.CoreHelpers.RunningOnWin7)
                {
                    TaskbarManager.Instance.SetOverlayIcon(My.Resources.Resources.open16, null);
                    TaskbarManager.Instance.SetProgressState(TaskbarProgressBarState.Normal);
                }
                int itemcount = 0;
                LoadFileDialog load = new LoadFileDialog();
                int @int = 0;
                foreach (string s in openfile.FileNames)
                {
                    itemcount++;
                }
                this.IsEnabled = false;
                load.Show();
                foreach (string i in openfile.FileNames)
                {
                    System.IO.FileInfo f = new System.IO.FileInfo(i);
                    NewDocument(f.Name);
                    SelectedDocument.Editor.LoadDocument(f.FullName);
                    SelectedDocument.Editor.FileChanged = false;
                    if (!MySettings.Default.Options_RecentFiles.Contains(SelectedDocument.Editor.DocumentName) && MySettings.Default.Options_RecentFiles.Count < 13)
                    {
                        MySettings.Default.Options_RecentFiles.Add(SelectedDocument.Editor.DocumentName);
                    }
                    if (MS.WindowsAPICodePack.Internal.CoreHelpers.RunningOnWin7)
                    {
                        TaskbarManager.Instance.SetProgressValue(@int, itemcount);
                        @int++;
                    }
                }
                this.IsEnabled = true;
                load.i = true;
                load.Close();
                if (MS.WindowsAPICodePack.Internal.CoreHelpers.RunningOnWin7)
                {
                    TaskbarManager.Instance.SetOverlayIcon(null, null);
                    TaskbarManager.Instance.SetProgressState(TaskbarProgressBarState.NoProgress);
                    TaskbarManager.Instance.TabbedThumbnail.InvalidateThumbnails();
                }
            }
        }
        else //Windows XP
        {
            Microsoft.Win32.OpenFileDialog openfile = new Microsoft.Win32.OpenFileDialog();
            openfile.Multiselect = true;
            openfile.Filter = "Supported Documents(.xaml,.html,.rtf,.txt)|*.xaml;*.html;*.rtf;*.txt|FlowDocument(.xaml)|*.xaml|Html Document(.html)|*.html|Rich Text Document(.rtf)|*.rtf|Plan Text Document(.txt)|*.txt|All Files(.)|*.";
            if (openfile.ShowDialog() == true)
            {
                LoadFileDialog load = new LoadFileDialog();
                load.Show();
                foreach (string i in openfile.FileNames)
                {
                    System.IO.FileInfo f = new System.IO.FileInfo(i);
                    NewDocument(f.Name);
                    SelectedDocument.Editor.LoadDocument(f.FullName);
                    SelectedDocument.Editor.FileChanged = false;
                    if (!MySettings.Default.Options_RecentFiles.Contains(SelectedDocument.Editor.DocumentName))
                    {
                        MySettings.Default.Options_RecentFiles.Add(SelectedDocument.Editor.DocumentName);
                    }
                }
                load.i = true;
                load.Close();
            }
        }
    }

    #endregion

    #region Close/Close All

    public void CloseMenuItem_Click(System.Object sender, RoutedEventArgs e)
    {
        if (SelectedDocument.Editor.FileChanged)
        {
            SaveFileDialog SaveDialog = new SaveFileDialog();
            TextRange tr = new TextRange(SelectedDocument.Editor.Document.ContentStart, SelectedDocument.Editor.Document.ContentEnd);
            System.IO.FileStream fs = System.IO.File.Open(My.MyWpfExtension.Computer.FileSystem.SpecialDirectories.Temp + "\\TVPre.xaml", System.IO.FileMode.Create);
            SaveDialog.Owner = this;
            SaveDialog.SetFileInfo(SelectedDocument.DocName, SelectedDocument.Editor);
            System.Windows.Markup.XamlWriter.Save(SelectedDocument.Editor.Document, fs);
            fs.Close();
            SaveDialog.ShowDialog();
            if (SaveDialog.Res == "Yes")
            {
                SaveMenuItem_Click(null, null);
                CloseDocument(SelectedDocument);
            }
            else if (SaveDialog.Res == "No")
            {
                CloseDocument(SelectedDocument);
            }
        }
        else
        {
            CloseDocument(SelectedDocument);
        }
    }

    public void CloseAllMenuItem_Click(object sender, RoutedEventArgs e)
    {
        while (TabCell.Items.Count > 0)
        {
            if (SelectedDocument.Editor.FileChanged == true)
            {
                SaveFileDialog save = new SaveFileDialog();
                TextRange tr = new TextRange(SelectedDocument.Editor.Document.ContentStart, SelectedDocument.Editor.Document.ContentEnd);
                System.IO.FileStream fs = System.IO.File.Open(My.MyWpfExtension.Computer.FileSystem.SpecialDirectories.Temp + "\\TVPre.xaml", System.IO.FileMode.Create);
                save.SetFileInfo(SelectedDocument.DocName, SelectedDocument.Editor);
                System.Windows.Markup.XamlWriter.Save(SelectedDocument.Editor.Document, fs);
                fs.Close();
                save.ShowDialog();
                if (save.Res == "Yes")
                {
                    SaveMenuItem_Click(this, null);
                    CloseDocument(SelectedDocument);
                }
                else if (save.Res == "No")
                {
                    CloseDocument(SelectedDocument);
                }
                else
                {
                    break;
                }
            }
            else
            {
                CloseDocument(SelectedDocument);
            }
        }
    }

    #endregion

    #region Save/Save As

    public void SaveMenuItem_Click(System.Object sender, RoutedEventArgs e)
    {
        if (SelectedDocument.Editor.DocumentName == null)
        {
            SaveAsMenuItem_Click(this, null);
        }
        else if (My.MyWpfExtension.Computer.FileSystem.FileExists(SelectedDocument.Editor.DocumentName))
        {
            SelectedDocument.Editor.SaveDocument(SelectedDocument.Editor.DocumentName);
        }
        UpdateButtons(this, e);

    }

    public void SaveAsMenuItem_Click(System.Object sender, RoutedEventArgs e)
    {
        if (MS.WindowsAPICodePack.Internal.CoreHelpers.RunningOnVista || MS.WindowsAPICodePack.Internal.CoreHelpers.RunningOnWin7)
        {
            CommonSaveFileDialog save = new CommonSaveFileDialog();
            CommonFileDialogFilter f = new CommonFileDialogFilter("FlowDocument", "*.xaml");
            CommonFileDialogFilter f2 = new CommonFileDialogFilter("Rich Text Document", "*.rtf");
            CommonFileDialogFilter f3 = new CommonFileDialogFilter("Text Document", "*.txt");
            CommonFileDialogFilter f4 = new CommonFileDialogFilter("All Files", "*.*");
            save.Filters.Add(f);
            save.Filters.Add(f2);
            save.Filters.Add(f3);
            save.Filters.Add(f4);
            if (save.ShowDialog() == CommonFileDialogResult.Ok)
            {
                if (save.SelectedFileTypeIndex == 1)
                {
                    SelectedDocument.Editor.SaveDocument(save.FileName + ".xaml");
                }
                else if (save.SelectedFileTypeIndex == 2)
                {
                    SelectedDocument.Editor.SaveDocument(save.FileName + ".rtf");
                }
                else if (save.SelectedFileTypeIndex == 3)
                {
                    SelectedDocument.Editor.SaveDocument(save.FileName + ".txt");
                }
                else
                {
                    SelectedDocument.Editor.SaveDocument(save.FileName);
                }
            }
        }
        else
        {
            Microsoft.Win32.SaveFileDialog save = new Microsoft.Win32.SaveFileDialog();
            save.Filter = "FlowDocument(.xaml)|*.xaml|Rich Text Document(.rtf)|*.rtf|Text Document|*.txt|All Files(.)|*.*";
            if (save.ShowDialog() == true)
            {
                SelectedDocument.Editor.SaveDocument(save.FileName);
            }
        }
        UpdateButtons(this, e);

    }

    #endregion

    #region Save All

    public void SaveAllMenuItem_Click(object sender, RoutedEventArgs e)
    {
        int @int = 0;
        if (MS.WindowsAPICodePack.Internal.CoreHelpers.RunningOnWin7)
        {
            TaskbarManager.Instance.SetOverlayIcon(My.Resources.Resources.save16, null);
            TaskbarManager.Instance.SetProgressState(TaskbarProgressBarState.Normal);
        }
        foreach (DocumentTab i in TabCell.Items)
        {
            if (i.Editor.DocumentName != null)
            {
                i.Editor.SaveDocument(i.Editor.DocumentName);
            }
            else
            {
                SaveAsMenuItem_Click(i, null);
            }
            if (MS.WindowsAPICodePack.Internal.CoreHelpers.RunningOnWin7)
            {
                TaskbarManager.Instance.SetProgressValue(@int, TabCell.Items.Count);
                @int++;
            }
        }
        if (MS.WindowsAPICodePack.Internal.CoreHelpers.RunningOnWin7)
        {
            TaskbarManager.Instance.SetOverlayIcon(null, null);
            TaskbarManager.Instance.SetProgressState(TaskbarProgressBarState.NoProgress);
        }
    }

    #endregion

    #region ---ExportMenuItem

    #region Export Xps

    public void ExportXPSMenuItem_Click(object sender, RoutedEventArgs e)
    {
        if (MS.WindowsAPICodePack.Internal.CoreHelpers.RunningOnVista || MS.WindowsAPICodePack.Internal.CoreHelpers.RunningOnWin7)
        {
            CommonSaveFileDialog save = new CommonSaveFileDialog();
            CommonFileDialogFilter f = new CommonFileDialogFilter("Xps Document", "*.xps");
            save.Filters.Add(f);
            if (save.ShowDialog() == CommonFileDialogResult.Ok)
            {
                System.Windows.Xps.Packaging.XpsDocument NewXpsDocument = new System.Windows.Xps.Packaging.XpsDocument(save.FileName + ".xps", System.IO.FileAccess.ReadWrite);
                System.Windows.Xps.XpsDocumentWriter xpsw = System.Windows.Xps.Packaging.XpsDocument.CreateXpsDocumentWriter(NewXpsDocument);
                xpsw.Write(SelectedDocument.Editor);
                NewXpsDocument.Close();
                xpsw = null;
            }
        }
        else
        {
            Microsoft.Win32.SaveFileDialog save = new Microsoft.Win32.SaveFileDialog();
            save.Filter = "Xps Document;*.xps";
            if (save.ShowDialog() == true)
            {
                System.Windows.Xps.Packaging.XpsDocument NewXpsDocument = new System.Windows.Xps.Packaging.XpsDocument(save.FileName + ".xps", System.IO.FileAccess.ReadWrite);
                System.Windows.Xps.XpsDocumentWriter xpsw = System.Windows.Xps.Packaging.XpsDocument.CreateXpsDocumentWriter(NewXpsDocument);
                xpsw.Write(SelectedDocument.Editor);
                NewXpsDocument.Close();
                xpsw = null;
            }
        }
    }

    #endregion






    #endregion

    public void BackgroundColorMenuItem_Click(object sender, RoutedEventArgs e)
    {
        ColorDialog d = new ColorDialog();
        d.Owner = this;
        if (d.ShowDialog() == true)
        {
            SolidColorBrush b = new SolidColorBrush(d.SelectedColor);
            SelectedDocument.Editor.Document.Background = b;
        }
    }



    public void ExitMenuItem_Click(object sender, RoutedEventArgs e)
    {
        Close();
    }

    #endregion

    #region --EditMenu

    #region Undo/Redo/Cut/Copy/Paste/Delete/Select All

    public void UndoMenuItem_Click(System.Object sender, RoutedEventArgs e)
    {
        SelectedDocument.Editor.Focus();
        SelectedDocument.Editor.Undo();
    }

    public void RedoMenuItem_Click(System.Object sender, RoutedEventArgs e)
    {
        SelectedDocument.Editor.Focus();
        SelectedDocument.Editor.Redo();
    }

    public void CutMenuItem_Click(System.Object sender, RoutedEventArgs e)
    {
        SelectedDocument.Editor.Focus();
        SelectedDocument.Editor.Cut();
    }

    public void CutParagraphMenuItem_Click(object sender, RoutedEventArgs e)
    {
        try
        {
            if (SelectedDocument.Editor.Selection.IsEmpty)
            {
                TextRange TRange = new TextRange(SelectedDocument.Editor.CaretPosition.Paragraph.ElementStart, SelectedDocument.Editor.CaretPosition.Paragraph.ElementEnd);
                if (!TRange.IsEmpty)
                {
                    SelectedDocument.Editor.Selection.Select(TRange.Start, TRange.End);
                    SelectedDocument.Editor.Cut();
                }
            }
            else
            {
                EditingCommands.MoveToLineStart.Execute(null, SelectedDocument.Editor);
                TextRange TRange = new TextRange(SelectedDocument.Editor.CaretPosition.Paragraph.ElementStart, SelectedDocument.Editor.CaretPosition.Paragraph.ElementEnd);
                if (!TRange.IsEmpty)
                {
                    SelectedDocument.Editor.Selection.Select(TRange.Start, TRange.End);
                    SelectedDocument.Editor.Cut();
                }
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
        e.Handled = true;
    }

    public void CutLineMenuItem_Click(object sender, RoutedEventArgs e)
    {
        EditingCommands.MoveToLineStart.Execute(null, SelectedDocument.Editor);
        EditingCommands.SelectToLineEnd.Execute(null, SelectedDocument.Editor);
        SelectedDocument.Editor.Cut();
        e.Handled = true;
    }

    public void CopyMenuItem_Click(System.Object sender, RoutedEventArgs e)
    {
        SelectedDocument.Editor.Focus();
        SelectedDocument.Editor.Copy();
    }

    public void CopyParagraphMenuItem_Click(object sender, RoutedEventArgs e)
    {
        try
        {
            if (SelectedDocument.Editor.Selection.IsEmpty)
            {
                TextRange TRange = new TextRange(SelectedDocument.Editor.CaretPosition.Paragraph.ElementStart, SelectedDocument.Editor.CaretPosition.Paragraph.ElementEnd);
                if (!TRange.IsEmpty)
                {
                    SelectedDocument.Editor.Selection.Select(TRange.Start, TRange.End);
                    SelectedDocument.Editor.Copy();
                }
            }
            else
            {
                EditingCommands.MoveToLineStart.Execute(null, SelectedDocument.Editor);
                TextRange TRange = new TextRange(SelectedDocument.Editor.CaretPosition.Paragraph.ElementStart, SelectedDocument.Editor.CaretPosition.Paragraph.ElementEnd);
                if (!TRange.IsEmpty)
                {
                    SelectedDocument.Editor.Selection.Select(TRange.Start, TRange.End);
                    SelectedDocument.Editor.Copy();
                }
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
        e.Handled = true;
    }

    public void CopyLineMenuItem_Click(object sender, RoutedEventArgs e)
    {
        EditingCommands.MoveToLineStart.Execute(null, SelectedDocument.Editor);
        EditingCommands.SelectToLineEnd.Execute(null, SelectedDocument.Editor);
        SelectedDocument.Editor.Copy();
        e.Handled = true;
    }

    #region Paste

    public void PasteMenuItem_Click(System.Object sender, RoutedEventArgs e)
    {
        SelectedDocument.Editor.Focus();
        SelectedDocument.Editor.Paste();
    }

    public void PasteTextButton_Click(object sender, RoutedEventArgs e)
    {
        SelectedDocument.Editor.CaretPosition.InsertTextInRun(Clipboard.GetText());
        e.Handled = true;
    }

    #endregion

    public void DeleteMenuItem_Click(object sender, RoutedEventArgs e)
    {
        EditingCommands.Delete.Execute(null, SelectedDocument.Editor);
    }

    public void DeleteParagraphMenuItem_Click(object sender, RoutedEventArgs e)
    {
        try
        {
            if (SelectedDocument.Editor.Selection.IsEmpty)
            {
                TextRange TRange = new TextRange(SelectedDocument.Editor.CaretPosition.Paragraph.ElementStart, SelectedDocument.Editor.CaretPosition.Paragraph.ElementEnd);
                if (!TRange.IsEmpty)
                {
                    SelectedDocument.Editor.Selection.Select(TRange.Start, TRange.End);
                    EditingCommands.Delete.Execute(null, SelectedDocument.Editor);
                }
            }
            else
            {
                EditingCommands.MoveToLineStart.Execute(null, SelectedDocument.Editor);
                TextRange TRange = new TextRange(SelectedDocument.Editor.CaretPosition.Paragraph.ElementStart, SelectedDocument.Editor.CaretPosition.Paragraph.ElementEnd);
                if (!TRange.IsEmpty)
                {
                    SelectedDocument.Editor.Selection.Select(TRange.Start, TRange.End);
                    EditingCommands.Delete.Execute(null, SelectedDocument.Editor);
                }
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
        e.Handled = true;
    }

    public void DeleteLineMenuItem_Click(object sender, RoutedEventArgs e)
    {
        EditingCommands.MoveToLineStart.Execute(null, SelectedDocument.Editor);
        EditingCommands.SelectToLineEnd.Execute(null, SelectedDocument.Editor);
        EditingCommands.Delete.Execute(null, SelectedDocument.Editor);
        e.Handled = true;
    }

    public void SelectAllMenuItem_Click(System.Object sender, RoutedEventArgs e)
    {
        SelectedDocument.Editor.Focus();
        SelectedDocument.Editor.SelectAll();
    }

    public void SelectParagraphMenuItem_Click(object sender, RoutedEventArgs e)
    {
        try
        {
            if (SelectedDocument.Editor.Selection.IsEmpty)
            {
                TextRange TRange = new TextRange(SelectedDocument.Editor.CaretPosition.Paragraph.ElementStart, SelectedDocument.Editor.CaretPosition.Paragraph.ElementEnd);
                if (!TRange.IsEmpty)
                {
                    SelectedDocument.Editor.Selection.Select(TRange.Start, TRange.End);
                }
            }
            else
            {
                EditingCommands.MoveToLineStart.Execute(null, SelectedDocument.Editor);
                TextRange TRange = new TextRange(SelectedDocument.Editor.CaretPosition.Paragraph.ElementStart, SelectedDocument.Editor.CaretPosition.Paragraph.ElementEnd);
                if (!TRange.IsEmpty)
                {
                    SelectedDocument.Editor.Selection.Select(TRange.Start, TRange.End);
                }
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
        e.Handled = true;
    }

    public void SelectLineMenuItem_Click(object sender, RoutedEventArgs e)
    {
        EditingCommands.MoveToLineStart.Execute(null, SelectedDocument.Editor);
        EditingCommands.SelectToLineEnd.Execute(null, SelectedDocument.Editor);
        e.Handled = true;
    }

    #endregion

    #region Find/Replace

    public void FindButton_Click(object sender, RoutedEventArgs e)
    {
        FindDialog findDialog = new FindDialog(SelectedDocument.Editor.Document);
        findDialog.Owner = this;
        findDialog.ShowDialog();
        if (findDialog.Res == "OK")
        {
            TextRange p = SelectedDocument.Editor.FindWordFromPosition(SelectedDocument.Editor.CaretPosition, findDialog.TextBox1.Text);
            try
            {
                SelectedDocument.Editor.Selection.Select(p.Start, p.End);
            }
            catch (Exception)
            {
                MessageBox.Show("Word not found.");
            }
        }
    }

    public void ReplaceButton_Click(object sender, RoutedEventArgs e)
    {
        ReplaceDialog replaceDialog = new ReplaceDialog();
        replaceDialog.Owner = this;
        replaceDialog.ShowDialog();
        if (replaceDialog.Res == "OK")
        {
            if (replaceDialog.TextBox1 != null)
            {
                TextRange p = SelectedDocument.Editor.FindWordFromPosition(SelectedDocument.Editor.Document.ContentStart.DocumentStart, (string)replaceDialog.TextBox1.Text);
                try
                {
                    SelectedDocument.Editor.Selection.Select(p.Start, p.End);
                    SelectedDocument.Editor.Selection.Text = (string)replaceDialog.TextBox2.Text;
                }
                catch (Exception)
                {
                    MessageBox.Show("Word not found.");
                }
            }
        }
    }

    public void CommonReplaceButton_Click(object sender, RoutedEventArgs e)
    {
        ReplaceButton_Click(null, null);
        e.Handled = true;
    }

    #endregion

    public void GoToButton_Click(object sender, RoutedEventArgs e)
    {
        GoToDialog gotodialog = new GoToDialog();
        gotodialog.Owner = this;
        gotodialog.ShowDialog();
        if (gotodialog.Res == "OK")
        {
            SelectedDocument.Editor.GoToLine(int.Parse(gotodialog.line));
        }
    }
    public void CommonGoToButton_Click(object sender, RoutedEventArgs e)
    {
        GoToButton_Click(null, null);
        e.Handled = true;
    }

    #endregion

    #region --ViewMenu


    public void ZoomInButton_Click(object sender, RoutedEventArgs e)
    {
        SelectedDocument.Editor.ZoomLevel += 0.1;
        ScaleTransform t = new ScaleTransform(SelectedDocument.Editor.ZoomLevel, SelectedDocument.Editor.ZoomLevel);
        SelectedDocument.Editor.LayoutTransform = t;
        UpdateButtons(this, e);

    }

    public void ZoomOutButton_Click(object sender, RoutedEventArgs e)
    {
        SelectedDocument.Editor.ZoomLevel += -0.1;
        ScaleTransform t = new ScaleTransform(SelectedDocument.Editor.ZoomLevel, SelectedDocument.Editor.ZoomLevel);
        SelectedDocument.Editor.LayoutTransform = t;
        UpdateButtons(this, e);

    }

    public void ResetZoomButton_Click(object sender, RoutedEventArgs e)
    {
        SelectedDocument.Editor.ZoomLevel = 1;
        ScaleTransform t = new ScaleTransform(SelectedDocument.Editor.ZoomLevel, SelectedDocument.Editor.ZoomLevel);
        SelectedDocument.Editor.LayoutTransform = t;
        UpdateButtons(null, null);

    }

    private WindowState fullscreensetting = System.Windows.WindowState.Normal;
    public void FullScreenMenuItem_Click(object sender, RoutedEventArgs e)
    {
        if (FullscreenMenuItem.IsPressed)
        {
            FullscreenMenuItem.IsChecked = false;
            Topmost = false;
            WindowStyle = System.Windows.WindowStyle.SingleBorderWindow;
            WindowState = fullscreensetting;
        }
        else
        {
            FullscreenMenuItem.IsChecked = true;
            fullscreensetting = WindowState;
            if (WindowState == System.Windows.WindowState.Maximized)
            {
                WindowState = System.Windows.WindowState.Normal;
            }
            WindowStyle = System.Windows.WindowStyle.None;
            Topmost = true;
            WindowState = System.Windows.WindowState.Maximized;
        }
    }

    #endregion

    #region Image

    public void ImageButton_Click(object sender, RoutedEventArgs e)
    {
        var id = new ImageDialog();
        id.Owner = this;
        id.ShowDialog();
        if (id.Res == "OK")
        {
            SelectedDocument.Editor.Focus();
            foreach (string i in id.Images)
            {
                TextPointer t = SelectedDocument.Editor.CaretPosition;
                Image img = new Image();
                BitmapImage b = new BitmapImage();
                b.BeginInit();
                b.UriSource = new Uri(i);
                b.EndInit();
                img.Height = b.Height;
                img.Width = b.Width;
                img.Source = b;
                InlineUIContainer inline = new InlineUIContainer(img);
                t.Paragraph.Inlines.Add(inline);
            }
        }
    }

    #endregion

    #region Link

    public void LinkMenuItem_Click(object sender, RoutedEventArgs e)
    {
        LinkDialog l = new LinkDialog();
        l.Owner = this;
        l.ShowDialog();
        if (l.Link != null)
        {
            //BUG:
            try
            {
                SelectedDocument.Editor.Focus();
                Hyperlink li = new Hyperlink(SelectedDocument.Editor.CaretPosition.DocumentStart, SelectedDocument.Editor.CaretPosition.DocumentEnd);
                Uri u = new Uri(l.Link);
                // += New RoutedEventHandler(h_Click)
                li.NavigateUri = u;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, ex.ToString());
            }
        }
    }

    #endregion

    #region Text File

    public void TextFileButton_Click(object sender, RoutedEventArgs e)
    {
        if (MS.WindowsAPICodePack.Internal.CoreHelpers.RunningOnVista || MS.WindowsAPICodePack.Internal.CoreHelpers.RunningOnWin7)
        {
            CommonOpenFileDialog o = new CommonOpenFileDialog();
            CommonFileDialogFilter f = new CommonFileDialogFilter("Text Document", "*.txt");
            o.Filters.Add(f);
            if (o.ShowDialog() == CommonFileDialogResult.Ok)
            {
                SelectedDocument.Editor.CaretPosition.InsertTextInRun(My.MyWpfExtension.Computer.FileSystem.ReadAllText(o.FileName));
            }
        }
        else
        {
            Microsoft.Win32.OpenFileDialog o = new Microsoft.Win32.OpenFileDialog();
            o.Filter = "Text Document|*.txt";
            if (o.ShowDialog() == true)
            {
                SelectedDocument.Editor.CaretPosition.InsertTextInRun(My.MyWpfExtension.Computer.FileSystem.ReadAllText(o.FileName));
            }
        }
    }

    #endregion

    #region Table

    public void TableButton_Click(object sender, RoutedEventArgs e)
    {
        TableDialog tableDialog = new TableDialog();
        tableDialog.Owner = this;
        tableDialog.ShowDialog();
        if (tableDialog.Res == "OK")
        {
            Table t = new Table();
            int @int = Convert.ToInt32(tableDialog.RowsTextBox.Text);
            int int2 = Convert.ToInt32(tableDialog.CellsTextBox.Text);
            while (@int != 0)
            {
                TableRowGroup trg = new TableRowGroup();
                TableRow tr = new TableRow();
                while (int2 != 0)
                {
                    TableCell tc = new TableCell();
                    if (tableDialog.BorderColor != null)
                    {
                        tc.BorderBrush = tableDialog.BorderColor;
                    }
                    else
                    {
                        tc.BorderBrush = Brushes.LightGray;
                    }
                    tc.BorderThickness = new Thickness(2, 2, 2, 2);
                    tr.Cells.Add(tc);
                    int2--;
                }
                int2 = Convert.ToInt32(tableDialog.CellsTextBox.Text);
                trg.Rows.Add(tr);
                t.RowGroups.Add(trg);
                @int--;
            }
            t.BorderBrush = Brushes.WhiteSmoke;
            if (SelectedDocument.Editor.CaretPosition.Paragraph.PreviousBlock != null)
            {
                SelectedDocument.Editor.Document.Blocks.InsertAfter(SelectedDocument.Editor.CaretPosition.Paragraph.PreviousBlock, t);
            }
            else
            {
                SelectedDocument.Editor.Document.Blocks.InsertAfter(SelectedDocument.Editor.CaretPosition.Paragraph.SiblingBlocks.FirstBlock, t);
            }
            UpdateButtons(this, e);
        }
    }

    #endregion

    #region Horizontal Line

    public void HorizontalLineButton_Click(object sender, RoutedEventArgs e)
    {
        InsertLineDialog d = new InsertLineDialog();
        d.Owner = this;
        d.ShowDialog();
        if (d.Res == "OK")
        {
            Separator line = new Separator();
            InlineUIContainer inline = new InlineUIContainer();
            line.Background = Brushes.Gray;
            line.Width = d.h;
            line.HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch;
            inline.Child = line;
            SelectedDocument.Editor.CaretPosition.Paragraph.Inlines.Add(inline);
        }
    }

    #endregion

    #region --FormatMenuItem

    public void ClearFormattingButton_Click(object sender, RoutedEventArgs e)
    {
        SelectedDocument.Editor.Selection.ClearAllProperties();
        UpdateSelected();
    }

    #region Font/Font Size/Font Color/Hightlight Color

    public void FontButton_Click(object sender, RoutedEventArgs e)
    {
        FontDialog d = new FontDialog();
        d.Owner = this;
        d.ShowDialog();
        if (d.Res == "OK")
        {
            SelectedDocument.Editor.Selection.ApplyPropertyValue(TextElement.FontFamilyProperty, d.font);
        }
    }

    public void FontSizeButton_Click(object sender, RoutedEventArgs e)
    {
        FontSizeDialog d = new FontSizeDialog();
        d.Owner = this;
        d.ShowDialog();
        if (d.Res == "OK" && d.Number > 0)
        {
            SelectedDocument.Editor.Selection.ApplyPropertyValue(TextElement.FontSizeProperty, d.Number);
        }
    }

    public void FontColorButton_Click(object sender, RoutedEventArgs e)
    {
        ColorDialog d = new ColorDialog();
        SolidColorBrush c = new SolidColorBrush();
        d.Owner = this;
        if (d.ShowDialog() == true)
        {
            c.Color = d.SelectedColor;
            SelectedDocument.Editor.Selection.ApplyPropertyValue(TextElement.ForegroundProperty, c);
        }
    }

    public void HighlightColorButton_Click(object sender, RoutedEventArgs e)
    {
        ColorDialog d = new ColorDialog();
        SolidColorBrush c = new SolidColorBrush();
        d.Owner = this;
        if (d.ShowDialog() == true)
        {
            c.Color = d.SelectedColor;
            SelectedDocument.Editor.Selection.ApplyPropertyValue(TextElement.BackgroundProperty, c);
        }
    }

    #endregion

    #region Blod/Italic/Underline/Strikethrough

    public void BoldMenuItem_Click(System.Object sender, RoutedEventArgs e)
    {
        EditingCommands.ToggleBold.Execute(null, SelectedDocument.Editor);
    }

    public void ItalicMenuItem_Click(object sender, RoutedEventArgs e)
    {
        EditingCommands.ToggleItalic.Execute(null, SelectedDocument.Editor);
    }

    public void UnderlineMenuItem_Click(object sender, RoutedEventArgs e)
    {
        EditingCommands.ToggleUnderline.Execute(null, SelectedDocument.Editor);
    }

    public void StrikethroughButton_Click(object sender, RoutedEventArgs e)
    {
        SelectedDocument.Editor.ToggleStrikethrough();
    }

    #endregion



    #region Indent More/Indent Less

    public void IndentMoreButton_Click(object sender, RoutedEventArgs e)
    {
        EditingCommands.IncreaseIndentation.Execute(null, SelectedDocument.Editor);
    }

    public void IndentLessButton_Click(object sender, RoutedEventArgs e)
    {
        EditingCommands.DecreaseIndentation.Execute(null, SelectedDocument.Editor);
    }

    #endregion

    #region Bullet List/Number List

    public void BulletListMenuItem_Click(object sender, RoutedEventArgs e)
    {
        EditingCommands.ToggleBullets.Execute(null, SelectedDocument.Editor);
    }

    public void NumberListMenuItem_Click(object sender, RoutedEventArgs e)
    {
        EditingCommands.ToggleNumbering.Execute(null, SelectedDocument.Editor);
    }

    #endregion

    #region Align Left/Center/Right/Justify

    public void AlignLeftMenuItem_Click(System.Object sender, RoutedEventArgs e)
    {
        EditingCommands.AlignLeft.Execute(null, SelectedDocument.Editor);
    }

    public void AlignCenterMenuItem_Click(System.Object sender, RoutedEventArgs e)
    {
        EditingCommands.AlignCenter.Execute(null, SelectedDocument.Editor);
    }

    public void AlignRightMenuItem_Click(System.Object sender, RoutedEventArgs e)
    {
        EditingCommands.AlignRight.Execute(null, SelectedDocument.Editor);
    }

    public void AlignJustifyMenuItem_Click(object sender, RoutedEventArgs e)
    {
        EditingCommands.AlignJustify.Execute(null, SelectedDocument.Editor);
    }

    #endregion

    #region Line Spacing

    public void LineSpacingButton_Click(object sender, RoutedEventArgs e)
    {
        LineSpacingDialog d = new LineSpacingDialog();
        d.Owner = this;
        d.ShowDialog();
        if (d.Res == "OK")
        {
            try
            {
                SelectedDocument.Editor.CaretPosition.Paragraph.LineHeight = d.number;
            }
            catch (Exception)
            {
                MessageBox.Show("Error");
            }
        }
    }

    #endregion

    #region ltr/rtl

    public void LefttoRightButton_Click(object sender, RoutedEventArgs e)
    {
        SelectedDocument.Editor.Document.FlowDirection = FlowDirection.LeftToRight;
    }

    public void RighttoLeftButton_Click(object sender, RoutedEventArgs e)
    {
        SelectedDocument.Editor.Document.FlowDirection = FlowDirection.RightToLeft;
    }

    #endregion

    #endregion

    #region --ToolsMenuItem

    public void SpellCheckButton_Click(object sender, RoutedEventArgs e)
    {
        SpellCheckDialog d = new SpellCheckDialog();
        TextPointer cp = SelectedDocument.Editor.CaretPosition;
        d.Owner = this;
        SpellingError sp = SelectedDocument.Editor.GetSpellingError(cp);
        if (sp != null)
        {
            foreach (string i in sp.Suggestions)
            {
                d.WordListBox.Items.Add(i);
            }
            d.WordListBox.SelectedIndex = 0;
            d.WordListBox.Focus();
        }
        d.ShowDialog();
        if (d.Res == "OK")
        {
            sp.Correct((string)d.WordListBox.SelectedItem);
        }
    }

    #endregion

    #region TabCell

    private void CloseDoc(object sender, RoutedEventArgs e)
    {
        TabHeader b = (TabHeader)e.Source;
        DocumentTab i = (DocumentTab)b.Parent;
        if (i.Editor.FileChanged)
        {
            SaveFileDialog SaveDialog = new SaveFileDialog();
            System.IO.FileStream fs = System.IO.File.Open(My.MyWpfExtension.Computer.FileSystem.SpecialDirectories.Temp + "\\TVPre.xaml", System.IO.FileMode.Create);
            TextRange tr = new TextRange(i.Editor.Document.ContentStart, i.Editor.Document.ContentEnd);
            SaveDialog.Owner = this;
            SaveDialog.SetFileInfo(i.DocName, i.Editor);
            System.Windows.Markup.XamlWriter.Save(i.Editor.Document, fs);
            fs.Close();
            SaveDialog.ShowDialog();
            if (SaveDialog.Res == "Yes")
            {
                SaveMenuItem_Click(this, null);
                CloseDocument(i);
            }
            else if (SaveDialog.Res == "No")
            {
                CloseDocument(i);
            }
        }
        else
        {
            CloseDocument(i);
        }
    }

    DocumentTab t;
    int oldPosition = -1;
    public void TabCell_PreviewMouseDown(object sender, MouseButtonEventArgs e)
    {
        if (e.Source.ToString() == "Document.Editor.TabHeader")
        {
            TabHeader h = (TabHeader)e.Source;
            t = (DocumentTab)h.Parent;
            object o = TabCell.ItemContainerGenerator.IndexFromContainer(h.Parent as DocumentTab);
            oldPosition = short.Parse(o.ToString());
        }
    }

    public void TabCell_PreviewMouseUp(object sender, MouseButtonEventArgs e)
    {
        if (e.Source.ToString() == "Document.Editor.TabHeader")
        {
            TabHeader h = (TabHeader)e.Source;
            object o = TabCell.ItemContainerGenerator.IndexFromContainer(h.Parent as DocumentTab);
            int i = short.Parse(o.ToString());
            if (o != null && oldPosition != i)
            {
                TabCell.Items.RemoveAt(oldPosition);
                TabCell.Items.Insert(i, t);
                TabCell.SelectedItem = t;
            }
        }
    }

    public void TabCell_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        if (TabCell.SelectedIndex == -1)
        {
            SelectedDocument = null;
            UpdateButtons(this, e);
        }
        else
        {
            SelectedDocument = (DocumentTab)TabCell.SelectedItem;
            if (MS.WindowsAPICodePack.Internal.CoreHelpers.RunningOnWin7)
            {
                if (TaskbarManager.Instance.TabbedThumbnail.IsThumbnailPreviewAdded(SelectedDocument.Editor))
                {
                    TaskbarManager.Instance.TabbedThumbnail.SetActiveTab(SelectedDocument.Editor);
                }
            }

            UpdateButtons(this, e);
        }
    }

    #endregion

    #region Windows 7

    private void preview_TabbedThumbnailMinimized(object sender, TabbedThumbnailEventArgs e)
    {
        this.WindowState = WindowState.Minimized;
    }

    private void preview_TabbedThumbnailMaximized(object sender, TabbedThumbnailEventArgs e)
    {
        this.WindowState = WindowState.Maximized;
    }

    private void preview_TabbedThumbnailClosed(object sender, TabbedThumbnailClosedEventArgs e)
    {
        //Dim editor As DocumentEditor = e.WindowsControl
        //Dim tab As DocumentTab = editor.Parent
        //If editor.FileChanged Then
        //    Dim s As New SaveFileDialog, fs As IO.FileStream = IO.File.Open(My.Computer.FileSystem.SpecialDirectories.Temp + "\TVPre.xaml", IO.FileMode.Create),
        //        tr As New TextRange(editor.Document.ContentStart, editor.Document.ContentEnd)
        //    s.SetFileInfo(tab.DocName, editor)
        //    Markup.XamlWriter.Save(editor.Document, fs)
        //    fs.Close()
        //    s.ShowDialog()
        //    If s.Res = "Yes" Then
        //        SaveMenuItem_Click(Me, Nothing)
        //        CloseDocument(tab)
        //    ElseIf s.Res = "No" Then
        //        CloseDocument(tab)
        //    Else
        //        e.Cancel = True
        //    End If
        //Else
        //    CloseDocument(tab)
        //End If
    }

    private void preview_TabbedThumbnailActivated(object sender, TabbedThumbnailEventArgs e)
    {
        DocumentEditor editor = (DocumentEditor)e.WindowsControl;
        DocumentTab tab = (DocumentTab)editor.Parent;
        TabCell.SelectedItem = tab;
        TaskbarManager.Instance.TabbedThumbnail.SetActiveTab(TaskbarManager.Instance.TabbedThumbnail.GetThumbnailPreview(editor));
        if (this.WindowState == WindowState.Minimized)
        {
            this.WindowState = WindowState.Normal;
        }
    }

    #endregion

    public void CloseAllButThisMenuItem_Click(object sender, RoutedEventArgs e)
    {
        //TODO:

    }
}
